package com.jwt.dao.impl;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.jwt.dao.AccountDAO;
import com.jwt.dao.bean.Account;

/**
 * 
 * @author Paul Kevin
 * 
 */

public class AccountDAOImpl extends SqlMapClientDaoSupport implements
		AccountDAO {
	
	public AccountDAOImpl(){
		
	}
	

	@Override
	public Account getAccount(String username) throws DataAccessException {
		return (Account) getSqlMapClientTemplate().queryForObject(
				"getAccountByUsername", username);
	}

	@Override
	public Account getAccount(String username, String password)
			throws DataAccessException {
		Account account = new Account();
		account.setUsername(username);
		account.setPassword(password);
		return (Account) getSqlMapClientTemplate().queryForObject(
				"getAccountByUsernameAndPassword", account);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List getUsernameList() throws DataAccessException {
		return getSqlMapClientTemplate().queryForList("getUsernameList", null);
	}

	@Override
	public void insertAccount(Account account) throws DataAccessException {
		getSqlMapClientTemplate().insert("insertAccount", account);
		getSqlMapClientTemplate().insert("insertProfile", account);
		getSqlMapClientTemplate().insert("insertSignon", account);

	}

	@Override
	public void updateAccount(Account account) throws DataAccessException {
		getSqlMapClientTemplate().update("updateAccount", account, 1);
		getSqlMapClientTemplate().update("updateProfile", account, 1);
		if (account.getPassword() != null && account.getPassword().length() > 0){
			getSqlMapClientTemplate().update("updateSignon", account, 1);
		}

	}

}
